package cn.dgut.training.controller;


import cn.dgut.training.dto.UserDTO;
import cn.dgut.training.entity.user.User;
import cn.dgut.training.mapper.TestMapper;
import cn.dgut.training.service.IUserService;
import cn.dgut.training.service.impl.UserServiceImpl;
import cn.dgut.training.vo.CommonResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserServiceImpl userService;

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public CommonResponse creat(@RequestBody User user) {
        userService.saveOrUpdate(user);
        return CommonResponse.success(user);
    }

    @GetMapping("/detail/{id}")
    public CommonResponse getById(@PathVariable("id") long id) {
        return CommonResponse.success(userService.getById(id));
    }


    @PutMapping("/update")
    public CommonResponse update(@RequestBody User user) {
        userService.updateById(user);
        return CommonResponse.message();
    }


    @PostMapping("/list")
    public CommonResponse listUserByPage(@RequestBody UserDTO userDTO) {
        return CommonResponse.success(userService.listUserByPage(userDTO));
    }

    @RequestMapping(value = "/openid", method = RequestMethod.POST)
    public CommonResponse getByOpenId(@RequestBody UserDTO userDTO) {
        return CommonResponse.success(userService.getByOpenId(userDTO));
    }

    @DeleteMapping("/{id}")
    public CommonResponse deleteById(@PathVariable("id") Long id) {
        userService.removeById(id);
        return CommonResponse.message();
    }

    @GetMapping("/iDetail")
    public User iGetUserById(@RequestParam("userId") Long userId) {
        return userService.getById(userId);

    }

}
